<?php defined("MAIN_DIR") or die("No direct access");

$add_error = false;
if (isset($_POST['action']) && $_POST['action'] == "add")
{
	$title = filter_var($_POST['title'],FILTER_SANITIZE_MAGIC_QUOTES);
	if (isset($_POST['by_agree']))
		$salary = 0;
	else
		$salary = filter_var($_POST['salary'],FILTER_SANITIZE_MAGIC_QUOTES);
	$languages = filter_var($_POST['languages'],FILTER_SANITIZE_MAGIC_QUOTES);
	$phone = filter_var($_POST['phone'],FILTER_SANITIZE_MAGIC_QUOTES);
	$fax = trim(filter_var($_POST['fax'],FILTER_SANITIZE_MAGIC_QUOTES));
	$email = trim(filter_var($_POST['email'],FILTER_SANITIZE_MAGIC_QUOTES));
	$experience = trim(filter_var($_POST['experience'],FILTER_SANITIZE_MAGIC_QUOTES));
	$demands = trim(filter_var($_POST['demands'],FILTER_SANITIZE_MAGIC_QUOTES));
	$acting = trim(filter_var($_POST['acting'],FILTER_SANITIZE_MAGIC_QUOTES));
	$add_query = "insert into `vacancy` (`category_id`,`title`,`bank_id`,`salary`,`education`,`languages`,`phone`,`fax`,`email`,`experience`,`demands`,`acting`) values ('$_POST[category]','$title','$_POST[bank]','$salary','$_POST[education]','$languages','$phone','$fax','$email','$experience','$demands','$acting')";
	if (mysql_query($add_query)) {
		$vacancy = mysql_fetch_array(mysql_query("select * from `vacancy` order by `vacancy_id` desc limit 1"));
		to("/admin/vacancies/edit/$vacancy[vacancy_id]");
	} else {
		$add_error = true;
	}
}

require_once ADMIN.HEADFILE;
?>
<script type="text/javascript">
$(document).ready(function(){
	$("#by_agree").click(function(){
		if ($(this).attr("checked"))
			$("#salary").attr("disabled","disabled");
		else
			$("#salary").removeAttr("disabled");
	})
})
</script>
<div align="left">
<h1>Добавить вакансию</h1>
<hr /><?php
if ($add_error)
	echo "<p style='color:red'>Не удалось добавить</p>";
?><form method='POST'>
<input type='hidden' name='action' value='add' />
<table cellpadding='5' cellspacing='0'>
	<tr>
		<td width="150px">Заголовок</td>
		<td width="700px">
			<input style='width:100%' type='text' name='title' value='<?php if (isset($_POST['title'])) echo $_POST['title'];?>' />
		</td>
	</tr>
	<tr>
		<td>Категория</td>
		<td>
			<select style="width:200px" name='category'><?php
			$category_res = mysql_query("select `category_id`,`title` from `vacancy_categories` order by `title`");
			while ($category_row = mysql_fetch_array($category_res))
			{
				echo "<option value='$category_row[category_id]'";
				if (isset($_POST['category']) && $category_row['category_id'] == $_POST['category'])
					echo " selected='selected'";
				echo ">$category_row[title]</option>";
			}
			?></select>
		</td>
	</tr>
	<tr>
		<td>Банк</td>
		<td>
			<select style="width:200px" name='bank'><?php
			$bank_res = mysql_query("select `bank_id`,`name` from `bank` where `city_id` = '$_SESSION[city_admin]' order by `name`");
			while ($bank_row = mysql_fetch_array($bank_res))
			{
				echo "<option value='$bank_row[bank_id]'";
				if (isset($_POST['bank']) && $bank_row['bank_id'] == $_POST['bank'])
					echo " selected='selected'";
				echo ">$bank_row[name]</option>";
			}
			?></select>
		</td>
	</tr>
	<tr>
		<td>Зарплата</td>
		<td>
			<input id="salary" style='width:200px' type='text' name='salary' value='<?php if (isset($_POST['salary'])) echo $_POST['salary'];?>' />
			<input type="checkbox" name='by_agree' id="by_agree" /> По договоренности
		</td>
	</tr>
	<tr>
		<td>Образование</td>
		<td>
			<select style="width:200px" name='education'><?php
			foreach ($education_array as $key => $value)
			{
				echo "<option value='$key'";
				if (isset($_POST['education']) && $key == $_POST['education'])
					echo " selected='selected'";
				echo ">$value</option>";
			}
			?></select>
		</td>
	</tr>
	<tr>
		<td>Знание языков</td>
		<td><input style="width:100%" type='text' name='languages' value='<?php if (isset($_POST['languages'])) echo $_POST['languages']?>' /></td>
	</tr>
	<tr>
		<td>Телефон</td>
		<td><input style='width:100%' type='text' name='phone' value='<?php if (isset($_POST['phone'])) echo $_POST['phone']?>' /></td>
	</tr>
	<tr>
		<td>Факс</td>
		<td><input style='width:100%' type='text' name='fax' value='<?php if (isset($_POST['fax'])) echo $_POST['fax']?>' /></td>
	</tr>
	<tr>
		<td>E-Mail</td>
		<td><input style='width:100%' type='text' name='email' value='<?php if (isset($_POST['email'])) echo $_POST['email'];?>' /></td>
	</tr>
	<tr valign='top'>
		<td>Опыт работы</td>
		<td><textarea style='width:100%' rows='5' name='experience'><?php
			if (isset($_POST['experience'])) echo $_POST['experience'];
		?></textarea></td>
	</tr>
	<tr valign='top'>
		<td>Требования</td>
		<td><textarea style='width:100%' rows='20' name='demands'><?php
			if (isset($_POST['demands'])) echo $_POST['demands'];
		?></textarea></td>
	</tr>
	<tr valign='top'>
		<td>Обязанности</td>
		<td><textarea style='width:100%' rows='20' name='acting'><?php
			if (isset($_POST['acting'])) echo $_POST['acting'];
		?></textarea></td>
	</tr>
	<tr valign='top'>
		<td colspan='2' align="right"><button style="width:100px;height:30px" type="submit">Добавить</button></td>
	</tr>
</table>
</form>
<br /><hr /><br />
</div>

<?php require_once ADMIN.FOOTERFILE; ?>